define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user.user/index',
                    add_url: 'user.user/add',
                    edit_url: 'user.user/edit',
                    // del_url: 'user.user/del',
                    multi_url: 'user.user/multi',
                    table: 'user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true},
                        {field: 'username', title: __('Username'), operate: 'LIKE'},
                        {field: 'avatar', title: __('Avatar'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                        {field: 'status', title: __('Status'), formatter: Table.api.formatter.status, searchList: {normal: __('Normal'), hidden: __('Hidden')}},
                        {field: 'truename', title: __('Truename'), operate: 'LIKE'},
                        {field: 'id_card', title: __('Id_card'), operate: 'LIKE'},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {field: 'idcard_front', title: __('Idcard_front'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                        {field: 'idcard_back', title: __('Idcard_back'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                        {field: 'is_certified', title: __('is_certified'), formatter: Table.api.formatter.label, searchList: {1:'未认证', 2:'待审核', 3:'已通过', 4:'已驳回'}},
                        {field: 'money', title: __('Money'), operate: 'between'},
                        {field: 'jointime', title: __('Jointime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'is_black', title: __('Is_black'), formatter: Table.api.formatter.label, searchList: {1:'否', 2:'是'}},
                        {field: 'black_reason', title: __('Black_reason'), operate: false, cellStyle: function () {return {css: {"max-width": "300px","white-space":"pre-line","word-wrap":"break-word","word-break":"break-all"}}}, align:"left"},
                        {field: 'user_order', title: __('Order record'),table: table,
                            buttons: [{
                                name: 'user_order',
                                text: function (row) {
                                    return '消费记录(' + row.order_num + ')';
                                },
                                title: "消费记录",
                                icon:"fa fa-list",
                                classname: 'btn btn-xs btn-primary btn-dialog',
                                url:function (row){
                                    return 'order/index?user_id=' + row.id;
                                }
                            }],
                            formatter: function (value,row,index){
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);
                                $(table).data("operate-edit", null);
                                $(table).data("operate-del", null);
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            },
                            events:Table.api.events.operate,operate:false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate, buttons: [
                                {
                                    name:'black',
                                    text:'拉黑',
                                    title:'拉黑',
                                    classname:'btn btn-xs btn-success btn-dialog',
                                    url: 'user.user/black?type=2',
                                    // confirm: '确认拉黑？',
                                    visible: function (row) {
                                        if(row.is_black == 1) return true;
                                        return false;
                                    },
                                    success:function (res){
                                        // console.log(res);
                                        table.bootstrapTable('refresh');
                                       Toastr.success("拉黑成功！");//成功后提示
                                        return false;

                                    },
                                    error: function (data, ret) {
                                        Toastr.error(ret.msg);//提示
                                        return false;
                                    },
                                },
                                {
                                    name:'white',
                                    text:'恢复',
                                    title:'恢复',
                                    classname:'btn btn-xs btn-danger btn-ajax',
                                    url: 'user.user/black?type=1',
                                    confirm: '确认恢复？',
                                    visible: function (row) {
                                        if(row.is_black == 2) return true;
                                        return false;
                                    },
                                    success:function (res){
                                        table.bootstrapTable('refresh');
                                        Toastr.success("恢复成功！");//成功后提示
                                        return false;
                                    },
                                    error: function (data, ret) {
                                        Toastr.error(ret.msg);//提示
                                        return false;
                                    },
                                },
                            ]}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        black: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});